package top.yzlin.jqueryjx3strategystation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.yzlin.jqueryjx3strategystation.dao.ArticleDAO;
import top.yzlin.jqueryjx3strategystation.dao.ReviewArticleDAO;
import top.yzlin.jqueryjx3strategystation.dao.UserDAO;
import top.yzlin.jqueryjx3strategystation.entity.community.ReviewArticle;
import top.yzlin.jqueryjx3strategystation.entity.vo.ResponseVO;

import java.util.Optional;

@RestController
public class AdminController {

    private ReviewArticleDAO reviewArticleDAO;
    private ArticleDAO articleDAO;
    private UserDAO userDAO;

    @Autowired
    public void setArticleDAO(ArticleDAO articleDAO) {
        this.articleDAO = articleDAO;
    }

    @Autowired
    public void setReviewArticleDAO(ReviewArticleDAO reviewArticleDAO) {
        this.reviewArticleDAO = reviewArticleDAO;
    }

    @Autowired
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @GetMapping("/admin/bandUser/list")
    public ResponseVO bandUserList() {
        return ResponseVO.createSuccessInstance(userDAO.findBanedUserList());
    }

    @PostMapping("/admin/bandUser/save")
    public ResponseVO bandUser(@RequestParam("userId") Integer userId) {
        return userDAO.setUserEnband(userId) > 0 ? ResponseVO.SUCCESS : ResponseVO.NOT_FOUND;
    }

    @PostMapping("/admin/bandUser/delete")
    public ResponseVO debandUser(@RequestParam("userId") Integer userId) {
        return userDAO.setUserDeband(userId) > 0 ? ResponseVO.SUCCESS : ResponseVO.NOT_FOUND;
    }

    @GetMapping("/admin/reviewArticle/list")
    public ResponseVO reviewArticleList() {
        return ResponseVO.createSuccessInstance(reviewArticleDAO.findAll());
    }

    @PostMapping("/admin/reviewArticle/save")
    public ResponseVO reviewArticleSave(@RequestParam("articleId")Integer articleId){
        if (reviewArticleDAO.isBanned(articleId)) {
            return ResponseVO.SUCCESS;
        } else {
            return articleDAO.findById(articleId).map(article -> {
                ReviewArticle reviewArticle = new ReviewArticle();
                reviewArticle.setArticle(article);
                reviewArticleDAO.save(reviewArticle);
                return ResponseVO.SUCCESS;
            }).orElse(ResponseVO.NOT_FOUND);
        }
    }

    @PostMapping("/admin/reviewArticle/pass")
    public ResponseVO reviewArticlePass(@RequestParam("reviewId") Integer reviewId) {
        reviewArticleDAO.deleteById(reviewId);
        return ResponseVO.SUCCESS;
    }

    @PostMapping("/admin/reviewArticle/delete")
    public ResponseVO reviewArticleDelete(@RequestParam("reviewId") Integer reviewId) {
        Optional<ReviewArticle> reviewArticleById = reviewArticleDAO.findById(reviewId);
        if (reviewArticleById.isPresent()) {
            ReviewArticle reviewArticle = reviewArticleById.get();
            reviewArticleDAO.deleteById(reviewId);
            articleDAO.deleteById(reviewArticle.getArticle().getArticleId());
            return ResponseVO.SUCCESS;
        } else {
            return ResponseVO.NOT_FOUND;
        }
    }

}
